package com.video.netty.send;

import com.video.commons.sendaop.ISocketSend;
import com.video.entity.Meeting;
import com.video.netty.commons.utils.ChannelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class MeetingSend extends ISocketSend {

    @Autowired
    private ChannelUtil channelUtil;

    @Override
    public Map<String, Object> sendMsg(Object... objs) {

        Meeting meeting = (Meeting) objs[0];
        String meetingInfo = getCreateMeeting(meeting);
        channelUtil.sendMsg(meetingInfo);

        return null;
    }

    /**
     * 发送会议创建信息
     * @param meeting
     */
    private String getCreateMeeting(Meeting meeting){
        StringBuilder sb = new StringBuilder();
        sb.append("{");

        if(meeting.getType().equals(0)){
            //主麦模式
            sb.append("\"id\":\"createOneToMany\",");
        } else {
            //多方讨论模式
            sb.append("\"id\":\"createGroup\",");
            sb.append("\"room\":\"" + meeting.getClient_start() + "\",");
        }

        sb.append("\"name\":\"" + meeting.getClient_start() + "\",");
        sb.append("\"users\":[");
        int j = 0;
        for (int i = 0; i < meeting.getClient_ids().length; i++) {
            Integer client_id = meeting.getClient_ids()[i];
            if(client_id.equals(meeting.getClient_start())){
                continue;
            }

            if(j != 0){
                sb.append(",");
            }

            sb.append("\"").append(client_id).append("\"");
            j++;
        }
        sb.append("]");
        sb.append("}");

        return sb.toString();
    }
}
